翻訳と辞書
Words near each other
・ Covenant Care
・ Covenant Chain
・ Covenant Christian Academy
・ Covenant Christian Academy (Georgia)
・ Covenant Christian Academy (Harrisburg, Pennsylvania)
・ Covenant Christian High School
・ Covenant Christian High School (DeMotte, Indiana)
・ Covenant Christian High School (Indianapolis, Indiana)
・ Covenant Christian High School (Michigan)
・ Covenant Christian School
・ Covenant Christian School (Canberra)
・ Covaresa
・ Covariance
・ Covariance (disambiguation)
・ Covariance and contravariance
Covariance and contravariance (computer science)
・ Covariance and contravariance of vectors
・ Covariance and correlation
・ Covariance function
・ Covariance group
・ Covariance intersection
・ Covariance mapping
・ Covariance matrix
・ Covariance operator
・ Covariant classical field theory
・ Covariant derivative
・ Covariant formulation of classical electromagnetism
・ Covariant Hamiltonian field theory
・ Covariant return type
・ Covariant transformation


Dictionary Lists
翻訳と辞書 辞書検索 [ 開発暫定版 ]
スポンサード リンク

Covariance and contravariance (computer science) : ウィキペディア英語版
Covariance and contravariance (computer science)
The type system of many programming languages support subtyping. For instance, if is subtype of , then an expression of type can be used whenever an expression of type could. Variance refers to how subtyping between more complex types (list of s versus list of s, function returning versus function returning , ...) relates to subtyping between their components. Depending on the variance of the type constructor, the subtyping relation may be either preserved, reversed, or ignored. For example, in C#:
* is a subtype of . The subtyping is preserved because is covariant on .
* is a subtype of . The subtyping is reversed because is contravariant on .
* Neither nor is a subtype of the other, because is invariant on .
The variance of a C# interface is determined by / annotations on its type parameters; the above interfaces are declared as , , and . Types with more than one type parameter may specify different variances on each type parameter. For example, the delegate type represents a function with a contravariant input parameter of type and a covariant return value of type .〔(Func Delegate ) - MSDN Documentation〕
The typing rules for interface variance ensure type safety. For example, an represents a first-class function expecting an argument of type , and a function that can handle any type of animal can always be used instead of one that can only handle cats.
A programming language designer will consider variance when devising typing rules for e.g. arrays, inheritance, and generic datatypes. By making type constructors covariant or contravariant instead of invariant, more programs will be accepted as well-typed. On the other hand, programmers often find contravariance unintuitive, and accurately tracking variance to avoid runtime type errors can lead to complex typing rules. In order to keep the type system simple and allow useful programs, a language may treat a type constructor as invariant even if it would be safe to consider it variant, or treat it as covariant even when that can violate type safety.
==Formal definition==

Within the type system of a programming language, a typing rule or a type constructor is:
* ''covariant'' if it preserves the ordering of types (≤), which orders types from more specific to more generic;
* ''contravariant'' if it reverses this ordering;
* ''bivariant'' if both of these apply (i.e., both ≤ and ≤ at the same time);
* ''invariant'' or ''nonvariant'' if neither of these applies.
The article considers how this applies to some common type constructors.

抄文引用元・出典: フリー百科事典『 ウィキペディア(Wikipedia)
ウィキペディアで「Covariance and contravariance (computer science)」の詳細全文を読む



スポンサード リンク
翻訳と辞書 : 翻訳のためのインターネットリソース

Copyright(C) kotoba.ne.jp 1997-2016. All Rights Reserved.